package com.check.utils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.hutool.core.lang.Validator;

/**
* @Title: Rule
* @Description:
* @author: zyt
* @date 2020年8月4日 下午4:50:41
*/

public class Rule {
	
	public boolean isNotNull(String str) {
	    str = str.trim();
		return !str.isEmpty();
	}
	
	public boolean emailRule(String str) {
        str = str.trim();
		String reg ="^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
		return Pattern.matches(reg, str);
	}
	
	public boolean phoneRule(String str) {
        str = str.trim();
		String reg ="^1[3|4|5|7|8][0-9]{9}$";
		return Pattern.matches(reg, str);
	}
	
	public boolean phoneAllRule(String str) {
        str = str.trim();
		String reg ="^1[3|4|5|7|8][0-9]{9}$";
		String tel = "^0\\d{2,3}-?\\d{7,8}$";
		return Pattern.matches(reg, str)||Pattern.matches(tel, str);
	}
	public static boolean IdentityCardVerification(String idStr){
        idStr = idStr.trim();
	    idStr = StringUtils.trim(idStr);
        return CertNoUtil.vId(idStr);
    }
    /**
     * 判断字符串是否为数字
     * @param str
     * @return
     */
    public boolean isStrNum(String str) {
        str = str.trim();
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (isNum.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 字母加数字
     */
    public boolean isNumAndAbc(String str) {
        str = str.trim();
    	String reg ="[a-zA-Z0-9 ]*";
		return Pattern.matches(reg, str);
    }
	/**
	 * 整数
	 */
	public boolean isIntNum(String str) {
        str = str.trim();
		String reg ="^(-?[1-9]\\d*)|0$";
		return Pattern.matches(reg, str);
	}

	/**
	 * 浮点数
	 */
	public boolean isDoubleNum(String str) {
        str = str.trim();
		String reg ="^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$";
		return Pattern.matches(reg, str);
	}
	
	private ArrayList<String> list  = new ArrayList<>();
	
	/**
	 * 唯一
	 */
	public boolean isOne(String str) {
        str = str.trim();
		if(this.list.contains(str)) {
			return false;
		}else {
			list.add(str);
			return true;
		}
	}
	
	/**
	 * 日期
	 */
	public boolean isDate(String str) {
        str = str.trim();
		if(str.length()==7||str.length()==6) {
			str = "20"+str;
			return Validator.isBirthday(str);
		}
		if(str.length()==8) {
			if(Validator.isBirthday(str)) {
				return true;
			}else {
				str = "20"+str;
				return Validator.isBirthday(str);
			}
		}
		return Validator.isBirthday(str);
	}
	
	/**
	 * 全都不是中文
	 */
	public boolean isNotAllChinese(String str) {
        str = str.trim();
		String reg = "[^\u4e00-\u9fa5]+";
		return Pattern.matches(reg, str);
	}

	public boolean matches(String reg,String str){
        str = str.trim();
	    return Pattern.matches(reg,str);
    }
	
	/**
	 * 日期+时间
	 */
	public boolean isDateTime(String str) {
        str = str.trim();
		String[] s = str.split(" ");
		if(s.length!=2) {
			return false;
		}
		if(!this.isDate(s[0])) {
			return false;
		}
		String[] s2 = s[1].split(":");
		if(s2.length!=2) {
			return false;
		}
		if(!Validator.isNumber(s2[0])&&!Validator.isBetween(Integer.parseInt(s2[0]),-1,24)) {
			return false;
		}
		if(!Validator.isNumber(s2[1])&&!Validator.isBetween(Integer.parseInt(s2[1]),-1,60)) {
			return false;
		}
		return true;
	}
}